from itertools import chain
from dateutil.relativedelta import relativedelta
import requests
import common.config
from common.config import *
from common.mysql_db_data import MysqlDbTriton


class Nationalheat:
    def getDataVersion(self,env):
        base_url = getconf(env, "data_url1")
        url = base_url + "triton/heatmap/getDataVersion"
        r = requests.get(url)
        print('最新数据时间',r.json()['data'])
        return r.json()

    def activePoint(self,env):
        base_url = getconf(env, "data_url1")
        url = base_url + "triton/heatmap/histogram/activePoint"
        r = requests.get(url)
        #print ('活跃点位数',r.json())
        return r.json()

    def activeChannel(self,env):
        base_url = getconf(env, "data_url1")
        url = base_url + "triton/heatmap/histogram/activeChannel"
        r = requests.get(url)
        #print('覆盖商场数', r.json())
        return r.json()

    def pesontime(self, env):
        base_url = getconf(env, "data_url1")
        url = base_url + "triton/heatmap/histogram/terminal/pesontime"
        r = requests.get(url)
        #print('屏前停留人次', r.json())
        return r.json()

    def interactive(self, env):
        base_url = getconf(env, "data_url1")
        url = base_url + "triton/heatmap/histogram/terminal/interactive"
        r = requests.get(url)
        #print('互动人次', r.json())
        return r.json()

    def getActiveMallDetail(self, env):
        base_url = getconf(env, "data_url1")
        url = base_url + "triton/heatmap/getActiveMallDetail"
        header={"Content-Type": "application/json;charset=UTF-8"}
        param={"current": 1, "size": 100}
        r = requests.post(url,data=json.dumps(param),headers=header)
        #print('活跃的商场明细', r.json())
        return r.json()

    def getHeatMapIndex(self, env,flag):
        base_url = getconf(env, "data_url1")
        url = base_url + "triton/heatmap/getHeatMapIndex"
        header={"Content-Type": "application/json;charset=UTF-8"}
        version=MysqlDbTriton(env)
        sql = "select max(dt) from ed_terminal_daily_summary where flag='1'"
        data = version.mysql_db_triton(sql)
        data = list(chain.from_iterable(data))
        start=str(data[0]-relativedelta(months=3))
        end=str(data[0])
        param={"flag":flag,"startDt":start,"endDt":end}
        r = requests.post(url,data=json.dumps(param),headers=header)
        #print('热力图数据', r.json())
        return r.json()


#a=Nationalheat()
#a.activePoint('test')